package com.cf.controller;


import com.cf.entity.IdCardConfirm;
import com.cf.entity.Result;
import com.cf.service.IdCardService;
import com.cf.util.Config;
import com.cf.util.HttpUtil;
import com.cf.util.RamdomNumber;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin
public class IdCardController {
    @Autowired
    private IdCardService idCardService;

    //    实名认证提交申请
    @RequestMapping("/addCard")
    public Object addCard(IdCardConfirm idCardConfirm){
        int i = idCardService.addCard(idCardConfirm);
        if(i!=0){
            return new Result(200,"申请成功",null);
        }else {
            return new Result(400,"申请失败",null);
        }
    }
    //    实名认证审核列表展示
    @RequestMapping("/selectCard")
    public Object selectCard(int pagenum,int pagesize,String name){
        Map map=new HashMap();
        map.put("name",name);
        PageInfo<IdCardConfirm> page = idCardService.selectCard(pagenum, pagesize, map);
        if (page.getList().size() != 0){
            return new Result(200,"查询用户信息成功",page);
        }else {
            return new Result(400,"查询用户信息失败",null);
        }
    }
    //    实名认证审核
    @RequestMapping("/updateCard")
    public Object updateCard(long id){
        int i = idCardService.updateCard(id);
        if(i!=0){
            return new Result(200,"审核通过",null);
        }else {
            return new Result(400,"审核失败",null);
        }
    }
    //    未通过删除当前申请
    @RequestMapping("/delCard")
    public Object delCard(long id){
        int i = idCardService.delCard(id);
        if(i!=0){
            return new Result(200,"审核不通过，清除当前申请",null);
        }else {
            return new Result(400,"失败",null);
        }
    }

    //    手机号验证
    @RequestMapping("/phoneYZ")
    public Object phoneYZ(String phone) throws Exception {
//        随机生成验证码
        String yam = RamdomNumber.getRandomNum();
        StringBuilder sb = new StringBuilder();
        sb.append("accountSid").append("=").append(Config.ACCOUNT_SID);
        sb.append("&to").append("=").append(phone);
        sb.append("&param").append("=").append(URLEncoder.encode(yam, "UTF-8"));
        sb.append("&templateid").append("=").append("258476");
        String body = sb.toString() +
                HttpUtil.createCommonParam(Config.ACCOUNT_SID, Config.AUTH_TOKEN);
        String result = HttpUtil.post(Config.BASE_URL, body);
        System.out.println(result);
        Result r=new Result();
        r.setCode(200);
        r.setMessage(yam);
        return r;
    }
    //    添加用户实名认证审核失败原因
    @RequestMapping("/updateReason")
    public Object updateReason(IdCardConfirm idCardConfirm){
        int i = idCardService.updateReason(idCardConfirm);
        if (i != 0) {
            return new Result(200, "修改成功", null);
        } else {
            return new Result(400, "修改失败", null);
        }
    }
    //    根据编号查询当前用户实名认证表
    @RequestMapping("/selectIdCardById")
    public Object selectIdCardById(long id){
        IdCardConfirm idCardConfirm = idCardService.selectIdCardById(id);
        if(idCardConfirm!=null){
            return new Result(200, "查询成功", idCardConfirm);
        }else {
            return new Result(200, "查询失败", null);
        }
    }

    //    根据编号查询当前用户实名认证表
    @RequestMapping("/selectICById")
    public Object selectIdById(long id){
        IdCardConfirm idCardConfirm = idCardService.selectIdById(id);
        if(idCardConfirm!=null){
            return new Result(200, "查询成功", idCardConfirm);
        }else {
            return new Result(400, "查询失败", null);
        }
    }


}
